package csc.daonjpa.java.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import csc.daonjpa.java.dao.CustomerDAO;
import csc.daonjpa.java.domain.Account;
import csc.daonjpa.java.domain.Customer;
/**
 * This is a controller for loginPage page when user login by loginId and Password
 * 
 * @author vuhuynh
 * 
 */
@Controller
public class CustomerController {

	@RequestMapping(value = "/getCustomerProfile")
	public String doSelectCustomerProfile(HttpServletRequest request,
			Model model) {
		// Read account info from request and save into Account object
		String loginId = request.getSession().getAttribute("loginSession")
				.toString();
		String message = "";
		if(request.getParameter("param") == null){
			message = "";
		}else{
			message = request.getParameter("param");
			
		}
		CustomerDAO cusDAO = new CustomerDAO();
		List<Customer> cus = cusDAO.getCustomerProfileByLoginId(loginId);
		model.addAttribute("customerProfile", cus);
		model.addAttribute("message", message);
		return ("updateProfile");

	}
	
	@RequestMapping(value = "/editProfile")
	public String doUpdateCustomerProfile(HttpServletRequest request,
			Model model) {
		String message="";
		// Read account info from request and save into Account object
		String loginId = request.getSession().getAttribute("loginSession")
				.toString();
		String fName = request.getParameter("firstName");
		String lName = request.getParameter("lastName");
		String address = request.getParameter("address");
		String phone = request.getParameter("phone");
		CustomerDAO cusDAO = new CustomerDAO();

		if (cusDAO.checkUpdateProfile(loginId, fName, lName, address, phone)) {
			System.out.println(" Update ok!!!");
			message="Update Successfully";
			
			return ("forward:/getCustomerProfile.html?param="+message);
		} else {
			System.out.println(" Update Failed");
			message="Update has Failed";
			
			return ("forward:/getCustomerProfile.html?param="+message);
		}

	}

	@RequestMapping(value = "/changePassword")
	public String changePassword(HttpServletRequest request,
			Model model) {
		// Read account info from request and save into Account object
		String loginId = request.getSession().getAttribute("loginSession")
				.toString();
		String message = "";
		if(request.getParameter("param") == null){
			message = "";
		}else{
			message = request.getParameter("param");
			
		}
		CustomerDAO cusDAO = new CustomerDAO();
		List<Customer> cus = cusDAO.getCustomerProfileByLoginId(loginId);
		model.addAttribute("customerProfile", cus);
		model.addAttribute("message", message);
		System.out.println(" " + loginId);
		return ("updatePassword");

	}

	@RequestMapping(value = "/updatePaswordPost")
	public String doUpdateCustomerPasword(HttpServletRequest request,
			Model model) {
		// Read account info from request and save into Account object
		String loginId = request.getSession().getAttribute("loginSession")
				.toString();
		String oldPass = request.getParameter("oldPass");
		String newPass = request.getParameter("newPass");
		String coNewPass = request.getParameter("coNewPass");
		CustomerDAO cusDAO = new CustomerDAO();
		boolean check = true;
		String message="";
		if(oldPass.equals("")){
			message+= "<br>Please Input Old Pass!!!</br>";
			check = false;
		}
		if(!cusDAO.checkLogin(loginId, oldPass)){
			message+= "<br>Old Pass is not correct!!!</br>";
			check = false;
		}
		if(newPass.equals("")){
			message+= "<br>Please Input New Pass!!!</br>";
			check = false;
		}
		if(coNewPass.equals("")){
			message+= "<br>Please Input Confirm New Pass!!!</br>";
			check = false;
		}
		if(!coNewPass.equals(newPass)){
			message+= "<br>Confirm New Pass is not the same to New Pass</br>";
			check = false;
		}
		if(check){
			if (cusDAO.checkUpdatePassword(loginId, newPass)) {
				message="Update Successfully!!!";
				model.addAttribute("message", message);
				return ("forward:/changePassword.htm?param="+message);
			} else {
				message="Update Failed!!!";
				model.addAttribute("message", message);
				return ("forward:/changePassword.htm?param="+message);
			}
		}else{
			model.addAttribute("message", message);
			return ("forward:/changePassword.html?param="+message);
		}
		

	}
}
